package cn.bgs.serviceutils.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    /**
     * 添加时自动填充
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        //自动填充方法，填充对象，填充类型，now()
        if (metaObject.hasSetter("createTime")){
            this.setFieldValByName("createTime",new Date(),metaObject);
        }
        if (metaObject.hasSetter("modifiedTime")){
            this.setFieldValByName("modifiedTime",new Date(),metaObject);
        }
        this.strictInsertFill(metaObject,"deleteFlag",Integer.class,0);

    }

    /**
     * 修改时自动填充
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        //自动填充方法，填充对象，填充类型，now()
        if (metaObject.hasSetter("modifiedTime")){
            this.setFieldValByName("modifiedTime",new Date(),metaObject);        }

    }
}
